package com.gdut.demo.controller;


import com.gdut.demo.entity.FWave;
import com.gdut.demo.service.FWaveService;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Controller
public class ConnectToServerController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private FWaveService fWaveService;


    /*
    * 采集器/用户触发业务服务器（采集器/用户向业务服务器发起请求）
    * 业务服务器处理这个请求，
    * 并在内部将数据转发给算法服务器
    * @username：采集器/用户的名字
    * */
    @GetMapping("/testForConnect")
    @ResponseBody
    public String testForConnect(String username) {
//        String url = "http://192.168.30.1:10002";
//        String url = "http://localhost:8888/test2";
        String url = "http://192.168.0.119:8080/register";
//        String url = "http://192.168.0.135:8083/register";
        HttpHeaders headers = new HttpHeaders();
        //定义请求参数为Json格式
        headers.setContentType(MediaType.APPLICATION_JSON);

        Map<String, Object> map = new HashMap<>();
        //将采集器/用户的名字保存
//        map.put("username",username);
//        map.put("Name","JR");

//        double[] onePacketDataArray = new double[17500];
        BigDecimal[] onePacketDataArray = new BigDecimal[17500];
        //获取数据
        List<FWave> onePacketData = fWaveService.getOnePacketData();
        for (int i = 0; i < onePacketData.size(); i++) {
//            System.out.println(onePacketData.get(i).getVoltage());
            onePacketDataArray[i] = onePacketData.get(i).getVoltage();
        }
        System.out.println(onePacketDataArray[1]);


//        map.put("ID",22222);
        map.put("data",onePacketDataArray);
        /*
        *  HttpEntity表示http的request和resposne实体，它由消息头和消息体组成。
        *  从HttpEntity中可以获取http请求头和回应头，也可以获取http请求体和回应体信息。
        *  HttpEntity的使用，与@RequestBody 、@ResponseBody类似。
        * */
        HttpEntity<Map<String ,Object>> request = new HttpEntity<>(map,headers);

        ResponseEntity<String> entity = restTemplate.postForEntity(url,request,String.class);
        HttpStatus statusCode = entity.getStatusCode();
//        System.out.println(statusCode);
        return String.valueOf(statusCode);
    }

    @PostMapping("/test2")
    @ResponseBody
//    public String test2(@RequestParam("username") String username) {
    public String test2(@RequestBody Map map) {
        System.out.println(map);
        System.out.println("test2执行");
        return "yes";

    }

}
